package com.gla.giliair.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gla.giliair.dao.CompanyDao;
import com.gla.giliair.entity.*;
import com.gla.giliair.service.CompanyService;
import com.gla.giliair.util.BeanUtil;
import com.gla.giliair.util.MapGenerator;
import com.gla.giliair.web.vo.Cus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: Yingtian qian
 * @Date: 2021/8/28 9:50
 */
@Service
public class CompanyServiceImpl implements CompanyService {
    @Autowired
    private CompanyDao companyDao;

    @Override
    public Company selectCompanyById(Long companyId) {
        if (companyId != null) {
            CompanyExample companyExample = new CompanyExample();
            companyExample.or().andCompanyIdEqualTo(companyId).andIsDeletedEqualTo(0);
            List<Company> companyList = companyDao.selectByExample(companyExample);
            if (companyList.isEmpty()) {
                return null;
            }
            return companyList.get(0);
        }
        return null;
    }

    @Override
    public List<Company> selectAllCompanies(CompanyExample orderBy, boolean showDeleted) {
        if (!showDeleted) {
            orderBy.or().andIsDeletedEqualTo(0);
        }
        List<Company> companyList = companyDao.selectByExample(orderBy);
        return companyList;
    }

    @Override
    public List<Company> selectCompaniesByParams(CompanyExample orderBy, boolean showDeleted, Cus searchParams) {
        CompanyExample.Criteria criteria = orderBy.createCriteria();
        if (!showDeleted) {
            criteria.andIsDeletedEqualTo(0);
        }
        if (StringUtils.hasText(searchParams.getCompanyName())) {
            criteria.andCompanyNameLike("%" + searchParams.getCompanyName() + "%");
        }
        if (StringUtils.hasText(searchParams.getCompanyCode())) {
            criteria.andCompanyCodeLike("%" + searchParams.getCompanyCode() + "%");
        }
        if (StringUtils.hasText(searchParams.getPhone())) {
            criteria.andPhoneLike("%" + searchParams.getPhone() + "%");
        }
        List<Company> companyList = companyDao.selectByExample(orderBy);
        return companyList;
    }

    @Override
    public Map<String, Object> findAllCompanies(String sort, boolean isASC, boolean showDeleted, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Company> companyList = selectAllCompanies(companySort(sort, isASC), showDeleted);
        PageInfo pageInfo = new PageInfo(companyList);
        if (!companyList.isEmpty()) {
            List<Cus> companyVOList = BeanUtil.copyList(companyList, Cus.class);
            pageInfo.setList(companyVOList);
            return MapGenerator.getResWithData(200, "查询航空公司列表成功！", pageInfo);
        } else {
            return MapGenerator.getRes(201, "查询结果为空！");
        }
    }

    @Override
    public Map<String, Object> addCompany(Company company) {
        CompanyExample companyExample = new CompanyExample();
        companyExample.or().andCompanyCodeEqualTo(company.getCompanyCode()).andIsDeletedEqualTo(0);
        List<Company> companyList = companyDao.selectByExample(companyExample);
        if (!companyList.isEmpty()) {
            return MapGenerator.getRes(502, "公司代号已被占用！");
        }
        //填充创建、更新日期
        Date nowDate = new Date();
        company.setCreateTime(nowDate);
        company.setUpdateTime(nowDate);
        if (companyDao.insertSelective(company) > 0) {
            return MapGenerator.getRes(200, "添加航空公司成功！");
        }
        return MapGenerator.getRes(520, "内部错误！");
    }

    @Override
    public Map<String, Object> modifyCompany(Company company) {
        Company companyFromDB = selectCompanyById(company.getCompanyId());
        if (companyFromDB == null) {
            return MapGenerator.getRes(502, "航空公司不存在！");
        }
        //把参数信息项中的非空字段合并到来自数据库的信息项中
        BeanUtil.copyOneIgnoreNull(company, companyFromDB);
        //覆盖更新日期
        companyFromDB.setUpdateTime(null);
        if (companyDao.updateByPrimaryKeySelective(companyFromDB) > 0) {
            return MapGenerator.getRes(200, "修改航空公司信息成功！");
        }
        return MapGenerator.getRes(520, "内部错误！");
    }

    @Override
    public Map<String, Object> deleteCompany(Long companyId) {
        Company companyFromDB = selectCompanyById(companyId);
        if (companyFromDB == null) {
            return MapGenerator.getRes(501, "航空公司不存在！");
        }
        companyFromDB.setIsDeleted(1);
        companyFromDB.setUpdateTime(null);
        if (companyDao.updateByPrimaryKeySelective(companyFromDB) > 0) {
            return MapGenerator.getRes(200, "删除航空公司成功！");
        }
        return MapGenerator.getRes(520, "内部错误！");
    }

    @Override
    public Map<String, Object> searchCompanies(String sort, boolean isASC, boolean showDeleted, Integer page, Integer pageSize, Cus searchParams) {
        if (searchParams == null) {
            return MapGenerator.getRes(500, "搜索参数为空！");
        }
        PageHelper.startPage(page, pageSize);
        List<Company> companyList = selectCompaniesByParams(companySort(sort, isASC), showDeleted, searchParams);
        PageInfo pageInfo = new PageInfo(companyList);
        if (!companyList.isEmpty()) {
            List<Cus> companyVOList = BeanUtil.copyList(companyList, Cus.class);
            pageInfo.setList(companyVOList);
            return MapGenerator.getResWithData(200, "查询航空公司列表成功！", pageInfo);
        } else {
            return MapGenerator.getRes(201, "查询结果为空！");
        }
    }

    //排序参数转化
    private CompanyExample companySort(String sort, boolean isASC) {
        CompanyExample companyExample = new CompanyExample();
        sort = sort == null ? "" : sort;
        String column = "";
        String order = "desc";
        if (isASC) {
            order = "asc";
        }
        switch (sort) {
            case "companyId":
                column = "company_id";
                break;
            case "companyName":
                column = "company_name";
                break;
            case "companyCode":
                column = "company_code";
                break;
            case "phone":
                column = "phone";
                break;
            case "url":
                column = "url";
                break;
            case "introduction":
                column = "introduction";
                break;
            case "createTime":
                column = "create_time";
                break;
            case "updateTime":
                column = "update_time";
                break;
            case "isDeleted":
                column = "is_deleted";
                break;
            default:
        }
        if ("".equals(column)) {
            companyExample.setOrderByClause("company_id desc");
        } else {
            if ("company_id".equals(column)) {
                companyExample.setOrderByClause(String.format("%s %s", column, order));
            } else {
                companyExample.setOrderByClause(String.format("%s %s , company_id desc", column, order));
            }
        }
        return companyExample;
    }
}
